from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from peliculas.models import Movie, Usuario, Biblioteca
from django.shortcuts import render
from forms import SignUpForm
import pdb

#definimos la vista main
def main(request):
    return render_to_response('main.html', {}, context_instance=RequestContext(request))

@login_required()
def home(request):
    UsuariosRegistrados = Usuario.objects.all()[:3]
    UltimasPeliculasSubidas = Movie.objects.filter(status = 3, classificationAdult = False)
    SaldoBiblioteca = Biblioteca.objects.filter(nombre = 'Biblioteca D11')
    context = {'UsuariosRegistrados':UsuariosRegistrados, 'UltimasPeliculasSubidas':UltimasPeliculasSubidas, 'SaldoBiblioteca': SaldoBiblioteca}
    #return render(request, 'home.html', {'user': request.user}, context)
    #return render_to_response('home.html', {'user': request.user}, context_instance=RequestContext(request))
    return render(request, 'home.html', context)

def signup(request):
    if request.method == 'POST':  # If the form has been submitted...
        form = SignUpForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass

            # Process the data in form.cleaned_data
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            email = form.cleaned_data["email"]
            first_name = form.cleaned_data["first_name"]
            last_name = form.cleaned_data["last_name"]

            # At this point, user is a User object that has already been saved
            # to the database. You can continue to change its attributes
            # if you want to change other fields.
            user = User.objects.create_user(username, email, password)
            user.first_name = first_name
            user.last_name = last_name

            # Save new user attributes
            user.save()

            return HttpResponseRedirect(reverse('main'))  # Redirect after POST
    else:
        form = SignUpForm()

    data = {
        'form': form,
    }
    return render_to_response('signup.html', data, context_instance=RequestContext(request))


def ingreso(request):
    if request.method == 'POST':
        ingreso = request.POST.get('ingreso')
        bibliotecaD11 = Biblioteca.objects.filter(nombre = 'Biblioteca D11')
        bibliotecaD11.dineroIngresado = bibliotecaD11.dineroIngresado + ingreso
        bibliotecaD11.save()
        return HttpResponseRedirect(reverse('main'))